<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>新建地址</title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/mui.picker.css" rel="stylesheet" />
		<link href="../css/mui.poppicker.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../fonts/iconfont/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../css/global.css" />
		<style type="text/css">
			.mui-bar-nav~.mui-content{padding-top: 0;}
			.mui-input-row label~input {
				text-align: right;
				float: right;
				padding: 11px 0;
			}
			
			.mui-input-row label {
				padding: 11px 0;
			}
			
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav bg_white">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title colorP">新建地址</h1>
			<button class="mui-btn-link mui-pull-right"><span class="colorP">保存</span></button>
		</header>
		<div class="mui-content">
			<ul class="mui-table-view bg_white margin-bottom10px" style="margin-top: 44px;">
				<li class="mui-table-view-cell">
					<div class="mui-input-row">
						<label class=" color999">收货人</label>
						<input class="name" type="text" id="user" placeholder="请输入收货人" value="">
					</div>
				</li>
				<li class="mui-table-view-cell">
					<div class="mui-input-row">
						<label class=" color999">手机号码</label>
						<input class="phoen" type="text" id="phone_number" placeholder="请输入电话号码" value="">
					</div>
				</li>
				<li class="mui-table-view-cell">
					<div class="mui-input-row" id="showCityPicker3">
						<label class=" color999">所在地区</label>
						<div class="mui-pull-right padding_tb11px">
							<span id="cityResult3">广东省 深圳市 宝安区</span>
							<i class="mui-icon iconfont">&#xe604;</i>
						</div>

					</div>
				</li>
				<li class="mui-table-view-cell padding0">
					<textarea name="" class="plac" rows="5" id="detail_address" cols="" placeholder="请输入详细地址"></textarea>
				</li>
			</ul>
		</div>
	</body>
	<script src="../js/mui.min.js"></script>
	<script src="../js/jquery.min.js"></script>
	<script src="../js/apppublic.js"></script>
	<script src="../js/mui.picker.js"></script>
	<script src="../js/mui.poppicker.js"></script>
	<script src="../js/apppublic.js"></script>
	<script src="../js/city.data.js" type="text/javascript" charset="utf-8"></script>
	<script src="../js/city.data-3.js" type="text/javascript" charset="utf-8"></script>
	<script>
		var showCityPickerButton = document.getElementById('showCityPicker3');
		var cityResult3 = document.getElementById('cityResult3');
		(function($, doc) {
			$.init();
			$.ready(function() {
				var cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData3);
				
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						if((items[2] || {}).text === undefined) {
							(items[2] || {}).text = '';
						}
						cityResult3.innerText = (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text;
						//返回 false 可以阻止选择框的关闭
						//return false; 
					});
				}, false);
			});
		})(mui, document);
		
		//收货人姓名不为空
		$('#user').blur(function(){
			if($(this).val() == ""){
				mui.toast("请输入收货人姓名");
				return;
			}
		})
		//收货人电话验证
		$('#phone_number').blur(function(){
			if($(this).val() != ""){
				if(!$(this).val().match(p1)){
					mui.toast("手机号码不正确，请重新输入");
					return;
				}
			} else{
				mui.toast("请输入手机号码");
				return;
			}
		})
		//收货人详细地址不为空
		$('#detail_address').blur(function(){
			if($(this).val() == ""){
				mui.toast("请输入详细地址");
				return;
			}
		})
		
		
		var userid,id,ws;
		document.addEventListener('plusready', function() {
			 ws = plus.webview.currentWebview();
			 id = ws.aid;
			console.log(id)
			if(id > 0) {
				$.ajax({
					type: 'post',
					url: apiRoot,
					data: { action: "Address.allAddressone",aid: id },
					dataTpye: 'json',
					success: function(dat) {
						console.log(JSON.stringify(dat))
						if(dat.error == 0) {
							var obj = dat.data;
							console.log(JSON.stringify(dat.data));

							$('.name').val(obj[0].name);
							$('.phoen').val(obj[0].mobile);
							var address = obj[0].address.split(' ');
							$('.plac').val(address[1]);
							$('#cityResult3').val(address[0])
						}

					},
					error: function(e) {

					}
				})
			}
			userid = plus.storage.getItem('uid');
		}, false)
		$('.colorP').click(function() {
			var name = $('.name').val();
			var phoen = $('.phoen').val();
			var plac = $('.plac').val();
			var cityResult3 = $('#cityResult3').html() + ' ' + plac;
			var parentid = plus.webview.getWebviewById('confirm_order.html');
        	mui.fire(parentid,'refresh_address',{u_name:name,u_phone:phoen,u_address:cityResult3});
			if(!phoen.match(p1) || !name.trim() || !plac.trim()) {
				plus.nativeUI.toast('请检查所填信息是否完整');
				return;
			}
			var loca;
			if(id>0){
				loca='edit';
				
			}else{
				loca="add";
				
			}
//			console.log(loca);
//			return;
			$.ajax({
				type: 'post',
				url: apiRoot,
				data: { action:'Address.action', type: loca,uid: userid,id:id ,name: name, mobile: phoen, address: cityResult3 },
				dataTpye: 'json',
				success: function(data){
					  if(data.error==0){
					  	toast('保存成功');
					  	setTimeout(function(){
						  plus.webview.getWebviewById(ws.w).reload();
				     	}, 1500);
					  }else{
					  	toast('保存失败')
					  }
				},
				error: function(e){
//					alert(JSON.stringify(e))
                    console.log(JSON.stringify(e))
				}
			})
		})
	</script>
</html>